package com.tledu.dangjian.controller;

import cn.hutool.db.Session;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tledu.dangjian.core.AjaxResult;
import com.tledu.dangjian.core.annotion.Auth;
import com.tledu.dangjian.core.constants.CommonConstants;
import com.tledu.dangjian.core.constants.ErrorCodeConstants;
import com.tledu.dangjian.core.exception.UsernameRepeatException;
import com.tledu.dangjian.model.Resource;
import com.tledu.dangjian.model.Role;
import com.tledu.dangjian.model.User;
import com.tledu.dangjian.service.IResourcesService;
import com.tledu.dangjian.service.IRoleService;
import com.tledu.dangjian.service.IUserService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/role")
@AllArgsConstructor
public class RoleController {

    private IRoleService roleService;
    private IResourcesService resourceService;

    @RequestMapping("/list")
    @Auth(auth = "role")
    public String userList(Role role, @RequestParam(required = true) Integer pageNum, @RequestParam(required = true) Integer pageSize, Model model) {
        PageHelper.startPage(pageNum, pageSize);
        model.addAttribute("pageInfo", PageInfo.of(roleService.list(role)));

        return "role/list";
    }

    @GetMapping("/add")
    public String add(Model model) {
        model.addAttribute("resourceList", resourceService.list(new Resource()));
        return "role/add";
    }

    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addActions(@RequestBody Role role, HttpSession httpSession) {
        User loginUser = (User) httpSession.getAttribute(CommonConstants.LOGIN_USER_SESSION_KEY);
        role.setCreatedBy(loginUser.getUserId());
        roleService.insert(role);
        return new AjaxResult("保存成功", null);
    }

    @DeleteMapping("/delete/{id}")
    @ResponseBody
    public AjaxResult delete(@PathVariable Integer id) {
        roleService.delete(id);
        return new AjaxResult("删除成功", null);
    }

    @GetMapping("/edit/{id}")
    public String editPage(@PathVariable Integer id, Model model) {
        Role role = roleService.selectById(id);
        model.addAttribute("entity", role);
        List<Integer> selectedResourceIdList = new ArrayList<>();

        for (Resource resource : role.getResourceList()) {
            selectedResourceIdList.add(resource.getResourceId());
        }
        model.addAttribute("selectedResourceIdList", selectedResourceIdList);
        model.addAttribute("resourceList", resourceService.list(new Resource()));
        return "role/edit";
    }

    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult edit(@RequestBody  Role role, HttpSession session) {
        // aop
        //  可以给所有的service的insert和update添加切面,设置创建人、更新人和更新时间
        //  获取user需要获取到session。
        //          spring 通过 ThreadLocal 帮我们存储了request

        User loginUser = (User) session.getAttribute(CommonConstants.LOGIN_USER_SESSION_KEY);
        role.setUpdatedBy(loginUser.getUpdatedBy());
        role.setUpdateTime(new Date());
        role.setCreatedBy(loginUser.getUserId());

        roleService.update(role);
        return new AjaxResult("修改成功", null);
    }

    @ExceptionHandler(value = UsernameRepeatException.class)
    @ResponseBody

    public AjaxResult handleException(UsernameRepeatException e) {
        return new AjaxResult(e.getMessage(), ErrorCodeConstants.USERNAME_REPEAT, null);
    }
}